package com.snap.vseries.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.adapter.HistoryAdapter;
import com.snap.vseries.base.BaseActivity;
import com.snap.vseries.bean.ExpEntity;
import com.snap.vseries.model.DBManager;
import com.snap.vseries.utils.TopTitleUtils;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * @author Rachel
 */
public class HisSearchResultActivity extends BaseActivity {
    @BindView(R.id.search_ll)
    LinearLayout searchLl;
    @BindView(R.id.search_tv)
    TextView searchTv;
    @BindView(R.id.rv)
    RecyclerView rv;
    private String searchString;
    private List<ExpEntity> mExpList;

    @Override
    protected int layoutResId() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    private void initData() {
        searchString = getIntent().getStringExtra("search_content");
    }

    private void initView() {
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        HistoryAdapter adapter = new HistoryAdapter(this, null);
        rv.setAdapter(adapter);
        adapter.setOnItemClickLitener((view, position) -> {
            Intent expResult = new Intent(this, ExpResultActivity.class);
            expResult.putExtra("fileName", mExpList.get(position).getName());
            startActivity(expResult);
        });
        new TopTitleUtils(this).setTitle(R.string.search_result);
        searchTv.setText(searchString);
        Observable.create((ObservableOnSubscribe<List<ExpEntity>>) emitter -> {
            List<ExpEntity> expList = DBManager.get().getExpByNameSync(searchString);
            emitter.onNext(expList);
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ExpEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<ExpEntity> expList) {
                        mExpList = expList;
                        adapter.updateData(expList);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        searchLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HisSearchResultActivity.this, HisSearchActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
